<?

$status = array(
  0  => "Unknown",
  1  => "Working",
  2  => "Complete",
  3  => "Verified",
  4  => "Rejected",
  5  => "Code Required"
  );

 

$menu_area=new Template("templates/menu/menu.exp.tmpl.php");

switch ($action) {
  case 0: // View Expansion Summary (Pass No Expansion Value)
      $body = new Template("templates/exp/exp.default.tmpl.php");
      $expansions = get_exp_summary();
      if ($expansions) {
        foreach ($expansions as $key=>$value) {
          $body->set($key, $value);
          }
        } 
      break;
  case 1: //View Zone Summaries Per Expansion 
      $body = new Template("templates/exp/exp.tmpl.php");
      $body->set('exp', $exp);
      $zones = get_exp_zone_summary();
      if ($zones) {
        foreach ($zones as $key=>$value) {
          $body->set($key, $value);
          }
        }     
    break;
  case 2: //View Quests Per Zone
      $body = new Template("templates/exp/exp.zone.tmpl.php");
      $body->set('z', $z);
      $body->set('exp', $exp);
      $body->set("status", $status);      
      $questsPerZone = getZoneQuests();
      if ($questsPerZone) {
        foreach ($questsPerZone as $key=>$value) {
          $body->set($key, $value);
          }
        }
    break;
  case 3: // Get Quest_ID
    check_authorization();
    $body = new Template("templates/exp/exp.add.tmpl.php");
    $body->set('suggest_quest_id', suggest_quest_id());
    $body->set('z', $z);
    $body->set("status", $status);
    $body->set("startzones", $startzones);
    $body->set('login', $login);
    break;
  case 4: // Delete Quest
    check_authorization();
    delete_quest();
    header("Location: index.php?editor=exp&z=$z&action=2");
    break;
  case 5: // Edit Quest
    check_authorization();
    $body = new Template("templates/exp/exp.edit.tmpl.php");
    $body->set('z', $z);
    $body->set('quest_id', $quest_id);
    $body->set("status", $status);
    $body->set("startzones", $startzones);
    $body->set('login', $login);
    $questinfo = quest_info();
      if ($questinfo) {
        foreach ($questinfo as $key=>$value) {
          $body->set($key, $value);
          }
        } 
      break;    
  case 6: // Update Quest
    check_authorization();
    update_quest();
    header("Location: index.php?editor=exp&z=$z&action=2");
    break;
  case 7: //Add Quest (From case 3 only for ID suggestion)
    check_authorization();
    add_quest();
    header("Location: index.php?editor=exp&z=$z&action=2");
    break;
  case 8: //Add NPC to Quest
  $javascript .= file_get_contents("templates/iframes/js.tmpl.php");
  $body = new Template("templates/exp/npc.add.tmpl.php");
    $body->set('z', $z);
    $body->set('quest_id', $quest_id);
    $body->set("status", $status);
    $body->set("startzones", $startzones);
    $body->set('login', $login);
    $questinfo = quest_info();
      if ($questinfo) {
        foreach ($questinfo as $key=>$value) {
          $body->set($key, $value);
          }
        } 
  $body->set('npcid', $_GET['npcid']);
  break;
  case 9: //Add NPC
    check_authorization();
    $body = new Template("templates/exp/exp.addnpc.tmpl.php");
    $body->set('startzone_id', $_REQUEST["startzone_id"]);
    $body->set('quest_id', $_REQUEST["quest_id"]);
    $body->set('quest_name', $_REQUEST["quest_name"]);
    $body->set('quest_status', $_REQUEST["quest_status"]);    
    $body->set("status", $status);
    $body->set("startzones", $startzones);
    $body->set('quest_developer', $_REQUEST["quest_developer"]);
    $body->set('last_modified', $_REQUEST["last_modified"]);
     $body->set('questgiver_npcid', $_REQUEST["npcresult"]);
    $body->set('login', $login);
      break;

}

function get_exp_summary() {
  global $mysql;
  $array = array();
  $query = "select expansion, sum((CASE quest_status WHEN '2' THEN 1 WHEN '3' THEN 1 WHEN '4' THEN 1 ELSE 0 END)) as completed, count(quest_id) as quests from qed_zone left join qed_quests on qed_zone.zoneidnumber=qed_quests.startzone_id where expansion >0 group by expansion order by expansion;";
  $result = $mysql->query_mult_assoc($query);
  if ($result) {
    foreach ($result as $result) {
      $array['expansions'][$result['expansion']] = array("expansion"=>$result['expansion'], "completed"=>$result['completed'], "quests"=>$result['quests']);
   }
 }
return $array;
}

function get_exp_zone_summary() {
  global $mysql;
  $exp = $_GET['exp'];
  $array = array();
  $query = "select short_name,sum((CASE quest_status WHEN '2' THEN 1 WHEN '3' THEN 1 WHEN '4' THEN 1 ELSE 0 END)) as completed, count(quest_id) as quests from qed_zone left join qed_quests on qed_zone.zoneidnumber = qed_quests.startzone_id where expansion =\"$exp\" group by short_name order by short_name;";
  $result = $mysql->query_mult_assoc($query);
  if ($result) {
    foreach ($result as $result) {
      $array['zones'][$result['short_name']] = array("short_name"=>$result['short_name'], "completed"=>$result['completed'], "quests"=>$result['quests']);
   }
 }
return $array;
}

function getZoneQuests() {
  global $mysql,$z;

//  $zid = getZoneID($z);  
  $array = array();
  $query = "select quest_id, quest_name, quest_status, quest_developer, date(last_modified) as last_modified from qed_quests  where startzone_id = \"$z\"order by quest_name;";
  $result = $mysql->query_mult_assoc($query);
  if ($result) {
    foreach ($result as $result) {
      $array['quests'][$result['quest_id']] = array("quest_id"=>$result['quest_id'], "quest_name"=>$result['quest_name'], "quest_status"=>$result['quest_status'], "quest_developer"=>$result['quest_developer'], "last_modified"=>$result['last_modified']);
   }
 }
return $array;
}

function quest_info() {
  global $mysql;
  
  $quest_id = $_GET['quest_id'];
  
  $query = "SELECT quest_id, quest_name, startzone_id,qed_zone.short_name, qed_zone.long_name, questgiver_npcid, quest_status, quest_developer, last_modified
FROM qed_quests join qed_zone on startzone_id=zoneidnumber where quest_id=\"$quest_id\"";
  $result = $mysql->query_assoc($query);
  return $result;
  }
  
function getStartZones() {
  global $mysql;

  $array = array();  
  $query = "SELECT zoneidnumber,short_name from qed_zone";
  $result = $mysql->query_mult_assoc($query);
  if ($result) {
    foreach ($result as $result) {
      $array['startzones'][$result['zoneidnumber']] = array("zoneidnumber"=>$result['zoneidnumber'], "short_name"=>$result['short_name']);
   }
 }
return $array;
}
  
function delete_quest() {  
  global $mysql;
  $qid = $_GET['qid'];

  $query = "DELETE from qed_quests WHERE quest_id=\"$qid\"";
  $mysql->query_no_result($query);
  }
  
function update_quest() {
  global $mysql;

  $quest_id = $_POST['quest_id'];
  $quest_name = $_POST['quest_name'];
  $startzone_id = $_POST['startzones'];
  $questgiver_npcid = $_POST['questgiver_npcid'];
  $quest_status = $_POST['quest_status'];
  $quest_developer = $_POST['developer'];

  
  $query = "UPDATE qed_quests SET quest_name=\"$quest_name\", startzone_id=\"$startzone_id\", questgiver_npcid=\"$questgiver_npcid\", quest_status=\"$quest_status\", quest_developer=\"$quest_developer\" WHERE quest_id=\"$quest_id\"";
  $mysql->query_no_result($query);
}  

function suggest_quest_id() {
  global $mysql;
  
  $query = "select max(quest_id) AS quest_id from qed_quests";
  $result = $mysql->query_assoc($query);
  
  return ($result['quest_id'] + 1);
  }
  
function add_quest() {
  global $mysql;
  
  $quest_id = $_POST['quest_id'];
  $quest_name = $_POST['quest_name'];
  $startzone_id = $_POST['startzones'];
  $questgiver_npcid = $_POST['questgiver_npcid'];
  $quest_status = $_POST['quest_status'];
  $quest_developer = $_POST['developer'];
  
  $query = "INSERT INTO qed_quests SET quest_id=\"$quest_id\", quest_name=\"$quest_name\", startzone_id=\"$startzone_id\", questgiver_npcid=\"$questgiver_npcid\", quest_status=\"$quest_status\", quest_developer=\"$quest_developer\"";
  $mysql->query_no_result($query);
} 
?>